<template>
  <div class="category-management">
    <h3>商品分类管理</h3>
    
    <el-button type="primary" @click="showAddDialog" style="margin-bottom: 20px;">
      添加分类
    </el-button>
    
    <el-table :data="tableData" border>
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="cateName" label="分类名称" />
      <el-table-column label="状态" width="100">
        <template #default="{ row }">
          <el-switch 
            v-model="row.state" 
            :active-value="1" 
            :inactive-value="0"
            @change="() => updateStatus(row)" 
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button size="small" @click="editCategory(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteCategory(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="10"
      @current-change="fetchCategories"
      style="margin-top: 20px;"
    />
    
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑分类' : '添加分类'" width="400px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="form.cateName" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.state" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  addCategory, 
  getCategory, 
  delCategory, 
  editCategory as apiEditCategory 
} from '@/api/account'

const tableData = ref([])
const total = ref(0)
const dialogVisible = ref(false)
const isEdit = ref(false)

const form = reactive({
  id: null,
  cateName: '',
  state: 1
})

// 获取分类列表
const fetchCategories = async (page = 1) => {
  try {
    const res = await getCategory({ currentPage: page, pageSize: 10 })
    tableData.value = res.data
    total.value = res.total
  } catch {
    ElMessage.error('获取分类失败')
  }
}

// 添加/编辑分类
const submitForm = async () => {
  if (!form.cateName) return ElMessage.warning('请输入分类名称')
  
  try {
    if (isEdit.value) {
      await apiEditCategory(form)
      ElMessage.success('更新成功')
    } else {
      await addCategory(form)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    fetchCategories()
  } catch {
    ElMessage.error('操作失败')
  }
}

// 删除分类
const deleteCategory = async (row) => {
  try {
    await ElMessageBox.confirm('确定删除此分类?', '提示', { type: 'warning' })
    await delCategory({ id: row.id })
    ElMessage.success('删除成功')
    fetchCategories()
  } catch {}
}

// 更新状态
const updateStatus = async (row) => {
  try {
    await apiEditCategory(row)
  } catch {
    row.state = row.state === 1 ? 0 : 1
    ElMessage.error('状态更新失败')
  }
}

// 显示编辑对话框
const editCategory = (row) => {
  Object.assign(form, row)
  isEdit.value = true
  dialogVisible.value = true
}

// 显示添加对话框
const showAddDialog = () => {
  form.id = null
  form.cateName = ''
  form.state = 1
  isEdit.value = false
  dialogVisible.value = true
}

onMounted(fetchCategories)
</script>

<style scoped>
.category-management {
  padding: 20px;
}
</style>